package com.fadadianqi.goods.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.goods.dao.GoodsDAO;
import com.fadadianqi.goods.model.Goods;
import com.yeqiangwei.commons.util.Pagination;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class GoodsDAOImpl extends GenericDAOSuport<String, Goods> implements GoodsDAO {

    @Override
    protected Class<?> getObjectClass() {
        return Goods.class;
    }

    @Override
    protected String getTableName() {
        return "goods";
    }

    @Override
    public List<Goods> findAll(Pagination pagination) {
        return super.findAll(pagination);
    }

    @Override
    public List<Goods> findByBrandId(String brandId, Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_BRAND_ID, brandId);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public List<Goods> findByPropertys(Map<String, Object> map) {
        return super.findByPropertys(map);
    }

    @Override
    public List<Goods> findByCategoryId(String categoryId, Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_CATEGORY_ID, categoryId);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public List<Goods> findByBrandIdAndCategoryId(String brandId,
                                                  String categoryId, Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_BRAND_ID, brandId);
        map.put(Goods.PROP_CATEGORY_ID, categoryId);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public Integer countByBrandIdAndCategoryId(String brandId, String categoryId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_CATEGORY_ID, categoryId);
        map.put(Goods.PROP_BRAND_ID, brandId);
        return super.countAll(map);
    }

    @Override
    public Integer countByCategoryId(String categoryId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_CATEGORY_ID, categoryId);
        return super.countAll(map);
    }

    @Override
    public Integer countByBrandId(String brandId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_BRAND_ID, brandId);
        return super.countAll(map);
    }

    @Override
    public Integer countAll() {
        return super.countAll(null);
    }

    @Override
    public Goods getByNameAndBrandIdAndCategoryId(String name, String brandId,
                                                  String categoryId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_BRAND_ID, brandId);
        map.put(Goods.PROP_CATEGORY_ID, categoryId);
        map.put(Goods.PROP_NAME, name);
        return super.getByPropertys(map);
    }

    @Override
    public Goods getByUniqueNameAndBrandIdAndCategoryId(String uniqueName,
                                                        String brandId, String categoryId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_BRAND_ID, brandId);
        map.put(Goods.PROP_CATEGORY_ID, categoryId);
        map.put(Goods.PROP_UNIQUE_NAME, uniqueName);
        return super.getByPropertys(map);
    }

    @Override
    public int countAllNotIndexed() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_INDEXED, false);
        return super.countAll(map);
    }

    @Override
    public List<Goods> findAllNotIndexed(Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(Goods.PROP_INDEXED, false);
        return super.findByPropertys(map);
    }

}
